package com.yonyou.pmclouds.comprehensiveacceptance.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.*;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.comprehensiveacceptance.entity.ComprehensiveAcceptanceVO;
import com.yonyou.pmclouds.comprehensiveacceptance.mapper.ComprehensiveAcceptanceVOMapper;
import com.yonyou.pmclouds.comprehensiveacceptance.process.*;
import com.yonyou.pmclouds.comprehensiveacceptance.rmiitf.ComprehensiveAcceptanceMaintain;
import com.yonyou.pmclouds.plan.plandriven.rmiitf.IScheduleDriven;
import com.yonyou.pmclouds.plan.processor.ScheduleDrivenProcessor;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ComprehensiveAcceptanceMaintain.class)
public class ComprehensiveAcceptanceMaintainImpl implements ComprehensiveAcceptanceMaintain, IFlowApproveBuziService {

    @Autowired
    private ComprehensiveAcceptanceVOMapper mapper;
    @Autowired
    private ComprehensiveAcceptanceBeforeInsertProcess acceptanceBeforeInsertProcess;
    @Autowired
    private ComprehensiveAcceptanceAfterInsertFillInspectItemInfoProcess acceptanceAfterInsertFillInspectItemInfoProcess;
    private static String[] UPDATE_STATUS = {ComprehensiveAcceptanceVO.BILLSTATUS};
    @Autowired
    private ComprehensiveAcceptanceAfterUpdateFillInspectItemInfoProcess afterUpdateFillInspectItemInfoProcess;
    @Autowired
    private ProjectStatusCheckProcessor<ComprehensiveAcceptanceVO> projectStatusCheckProcessor;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<ComprehensiveAcceptanceVO> billCountFactory;
    @Autowired
    private AcceptanceApprovedWriteBackProjectProcessor writeBackProjectProcessor;
    @Autowired
    private BillDrivenProcessorFactory drivenProcessorFactory;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<ComprehensiveAcceptanceVO> billForUpdateProcessorFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<ComprehensiveAcceptanceVO> billCountDeleteFactory;
    @Autowired
    private ComprehensiveAcceptanceProjectLogPostProcess comprehensiveAcceptanceProjectLogPostProcess;
    @Autowired
    private IScheduleDriven scheduleDriven;
    @Autowired
    private DocCenterProcessor docCenterProcessor;

    private static String[] UPDATE_FIELDS = {ComprehensiveAcceptanceVO.PK_CONSTRUCTIONUNIT, ComprehensiveAcceptanceVO.ACCEPTANCETIME, ComprehensiveAcceptanceVO.BILLSTATUS};

    @Override
    @PMApprove
    public ComprehensiveAcceptanceVO insert(ComprehensiveAcceptanceVO acceptanceVO) throws BusinessException {
        SingleInsertBpTemplate<ComprehensiveAcceptanceVO> bp = new SingleInsertBpTemplate<ComprehensiveAcceptanceVO>(mapper);
        bp.addBeforeProcessor(acceptanceBeforeInsertProcess);
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<ComprehensiveAcceptanceVO>());

        bp.addBeforeProcessor(projectStatusCheckProcessor);
        // 综合验收单保存后保存表体检查项选择的结果
        bp.addAfterProcessor(acceptanceAfterInsertFillInspectItemInfoProcess);

        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE));
        //智能日志
        bp.addAfterProcessor(comprehensiveAcceptanceProjectLogPostProcess);
        bp.insert(acceptanceVO);

        return acceptanceVO;
    }

    @Override
    @PMApprove(trigger = "commit")
    public ComprehensiveAcceptanceVO update(ComprehensiveAcceptanceVO acceptanceVO) throws BusinessException {
        SingleUpdateBpTemplate<ComprehensiveAcceptanceVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        bp.addBeforeProcessor(acceptanceBeforeInsertProcess);
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ComprehensiveAcceptanceVO>());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        // 综合验收单保存后保存表体检查项选择的结果
        bp.addAfterProcessor(afterUpdateFillInspectItemInfoProcess);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE));
        bp.update(acceptanceVO);
        return acceptanceVO;
    }

    @Override
    public ComprehensiveAcceptanceVO approve(String pkComprehensiveAcceptance, byte billStatus) throws BusinessException {
        if (StringUtils.isEmpty(pkComprehensiveAcceptance)) {
            return null;
        }
        ComprehensiveAcceptanceVO comprehensiveAcceptanceVO = mapper.getById(pkComprehensiveAcceptance);
        SingleUpdateBpTemplate<ComprehensiveAcceptanceVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);

        comprehensiveAcceptanceVO.setBillstatus(billStatus);
        // 验收日期回写项目日期
        bp.addAfterProcessor(writeBackProjectProcessor);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE));
        // 驱动计划
        bp.addAfterProcessor(new ScheduleDrivenProcessor<ComprehensiveAcceptanceVO>(scheduleDriven));

        // 单据驱动项目状态变化
        bp.addAfterProcessor(drivenProcessorFactory.getBillDrivenProcessor(BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE,
                (BillDrivenProcessorFactory.AdditionalInfoToDriven<ComprehensiveAcceptanceVO>) null));

        bp.addAfterProcessor(docCenterProcessor);

        bp.update(comprehensiveAcceptanceVO);

        return comprehensiveAcceptanceVO;
    }

    @Override
    public ComprehensiveAcceptanceVO reject(String pkComprehensiveAcceptance) throws BusinessException {
        if (StringUtils.isEmpty(pkComprehensiveAcceptance)) {
            return null;
        }
        ComprehensiveAcceptanceVO comprehensiveAcceptanceVO = mapper.getById(pkComprehensiveAcceptance);
        SingleUpdateBpTemplate<ComprehensiveAcceptanceVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        comprehensiveAcceptanceVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE));
        bp.update(comprehensiveAcceptanceVO);
        return comprehensiveAcceptanceVO;
    }


    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        return approve(pk_bill, billStatus);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        return reject(pk_bill);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        ComprehensiveAcceptanceVO comprehensiveAcceptanceVO = mapper.getById(pk_bill);
        return comprehensiveAcceptanceVO;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<ComprehensiveAcceptanceVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(mapper);
        deleteBpTemplate.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE));
        return deleteBpTemplate.delete((ComprehensiveAcceptanceVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
